package function.playlist;

import tool.FileConnectionUtils;
import tool.Path;
import db.DBListener;
import db.DBReader;
import db.Mp3DBRecord;
import function.Mp3File;

public class NowPlaylist extends SimplePlaylist {

	public NowPlaylist(String type) {
		super(type);
	}

	public String getType() {
		return genre;
	}

	public int getSize() {
		return size;
	}

	protected void calculateSize() {
		size = FileConnectionUtils.getNum(Path.NOW, "*.link");
	}

	public Mp3File getMp3(int index) {
		String filename = FileConnectionUtils.getFileAt(Path.NOW, index, "*.link");

		DBReader dbReader = new DBReader(Path.NOW + "/" + filename);
		InnerListener listener = new InnerListener();
		dbReader.addListener(listener);
		dbReader.readDB();
		Mp3DBRecord record = listener.getRecord();

		return record.getMp3File();
	}
	
	private class InnerListener implements DBListener {

		private Mp3DBRecord record;

		public void processRecord(Mp3DBRecord currentRecord) {
			record = currentRecord;
		}

		public void readingDone() {
			// Do nothing
		}
		
		public Mp3DBRecord getRecord() {
			return record;
		}
	}
}
